<?php

# http://projects:8081/cdapp-2kt-api/api/journal_visit.php

include_once '_portal.php';

$input = $post->input;
$action = $post->action;
if (!$input || !$action) {
  die();
}

if ($action == "add") {
  output(handle_add($input));
} elseif ($action == "query") {
  output(handle_query($input));
}
die();

function handle_add($input) {
  create_table();
  $sql = "INSERT INTO `2kt_journal_visit`(`visitorName`, `enterTime`, `leaveTime`, `visitDuration`) VALUES ('{$input->visitorName}','{$input->enterTime}','{$input->leaveTime}',{$input->visitDuration})";
  $result = exec_sql($sql);
  return $result;
}

function handle_query($input) {
  create_table();
  $sql = "SELECT * FROM `2kt_journal_visit` WHERE `enterTime` <= '{$input->end}' AND  `leaveTime` >= '{$input->start}'";
  $result = exec_sql($sql);
  return $result;
}

function create_table() {
  // $sql = "CREATE TABLE IF NOT EXISTS `2kt_journal_visit` (
  //   `number` int(8) NOT NULL AUTO_INCREMENT,
  //   `visitorName` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
  //   `enterTime` datetime NOT NULL,
  //   `leaveTime` datetime NOT NULL,
  //   `visitDuration` int(8) NOT NULL,
  //   PRIMARY KEY (`number`)
  //  ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  // $result = exec_sql($sql);
  // return $result;
}